/**
 * Created by kitabi on 2017/6/20.
 */
/**
 * Created by kitabi on 2017/6/16.
 */
var classify={

};

var $btnStaffConfirm= $("#btnStaffConfirm");
var $staffDialog= $("#staffDialog");
/**
 * 获取用户数据
 */
classify.queryList= function(){


    $("#mainTable").bootstrapTable({

        method:'POST',
        dataType:'json',
        contentType: "application/x-www-form-urlencoded",
        cache: false,
        striped: true,                              //是否显示行间隔色
        sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
        url:"/pull/qryVideoList",
        // height: $(window).height() - 110,
        // width:$(window).width(),
        // showColumns:true,
        pagination:true,
        // queryParams : queryParams,
        minimumCountColumns:2,
        pageNumber:1,                       //初始化加载第一页，默认第一页
        pageSize: 10,                       //每页的记录行数（*）
        pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
        uniqueId: "id",                     //每一行的唯一标识，一般为主键列
        showExport: true,
        exportDataType: 'all',


        paginationPreText:"<",
        paginationNextText:">",
        queryParams: function(params) {

            var userSearch= $("#userSearch").val();
            var userDateStart=$("#userDateStart").val();
            var userDateEnd=$("#userDateEnd").val();
            return {
                cPage: (params.offset/params.limit)+1,
                pSize: params.limit,
                classify:$("#classifylist").val(),
                status:$("#userStatus").val(),
                title:userSearch,
                start:userDateStart,
                end:userDateEnd

            };
        },
        columns: [
            {
                field: 'state',
                checkbox: true,
                align: 'center',
                valign: 'middle'
            },{
                field: 'id',
                title: 'ID'
            }, {
                field: 'img_url',
                title: '封面图片',
                formatter: function(value,row,index){
                    //value：当前field的值，即id
                    //row：当前行的数据
                    var img_url= row.img_url;

                    return  '<img src="'+img_url+'" style="width: 40px;height: 40px;">';
                }
            },{
                field: 'title',
                title: '标题'
            },{
                field: 'create_time',
                title: '创建时间'
            }, {

                field: 'status',
                title: '操作',
                align: 'center',

                formatter: function(value,row,index){
                    //value：当前field的值，即id
                    //row：当前行的数据
                    var id= row.id;

                    var add='<button type="button" onclick="showDetail('+id+',\'update\')" style="margin-right: 5px;" class="btn btn-sm btn-primary btn-trans">详情</button>';

                    var e='<button type="button" onclick="showDetail('+id+',\'update\')" style="margin-right: 5px;" class="btn btn-sm btn-info btn-trans">编辑</button>';
                    var d='<button type="button" onclick="del('+id+')" style="margin-right: 5px;" class="btn btn-sm btn-info btn-trans">删除</button>';
                    return  add+e+d;
                }
            }]

    });

}

function updateStatus(id ,type,version){
    var param={
        id: id,
        status: type,
        version:version
    }
    Ajax.ajax({
        url: "/offer/updateStatus",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert("操作失败");
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function del(id){
    var param = {
        id:id
    }
    Ajax.ajax({
        url: "/pull/deleteVideo",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                // alert(res.data);
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert(res.data);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function addType() {
    var brandName = $("#Sname").val();
    var indexs= $("#indexs").val();
    if(brandName == ""){
        alert("请输入品牌名称");
        return;
    }
    var param = {
        brandName:brandName,
        indexs:indexs
    }
    Ajax.ajax({
        url: "/offertype/add",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                alert(res.data);
                clearDialog();
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert(res.data);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function clearDialog() {
    $("#Sname").val("");
    $("#indexs").attr("data-id","");
    $("#indexs").val("");
    $("#userSearch").val();
    $staffDialog.hide();
}

function showDetail(id,type){

    if(type == 'add'){
        $("#dialogMain").load("/pull/qryVideoDetail");
    }else{
        $("#dialogMain").load("/pull/qryVideoDetail?id="+id);
    }


    $staffDialog.modal('show');

}
function updateType() {
    var id = $("#indexs").attr('data-id');
    var offerTypeName = $("#Sname").val();
    var indexs= $("#indexs").val();
    if(offerTypeName == ""){
        alert("请输入名称");
        return;
    }
    var param = {
        id:id,
        offerTypeName:offerTypeName,
        indexs:indexs
    }
    Ajax.ajax({
        url: "/offertype/update",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                alert(res.data);
                clearDialog();
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert(res.data);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function queryClass(){
    var param = {

    }
    Ajax.ajax({
        url: "/offertype/gryTypeListAll",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            for(var i=0;i<res.length;i++){
                var option=$("<option value='"+res[i].id+"'>"+res[i].type_name+"</option>");
                $("#classifylist").append(option);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

$(function(){
    //关闭窗口
    $('#staffDialogClose').on('click',function(){

        $staffDialog.hide();
    });

    classify.queryList();

    queryClass();

    $("#userSearchBtn").click(function(){
        $("#mainTable").bootstrapTable('refresh');
    });

    //开始\结束 日期选择
    $("#userDateStart").datepicker({
        format: 'yyyy-mm-dd ', language: 'zh-CN',                 autoclose: true,                 todayHighlight: true
    });
    $("#userDateEnd").datepicker({
        format: 'yyyy-mm-dd ',  language: 'zh-CN',                 autoclose: true,                 todayHighlight: true
    });
});